﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenAuth.App.OAuth2
{
    public class OAuth2LoginUI
    {
        /// <summary>
        /// 获取前台显示HTML块
        /// </summary>
        /// <returns></returns>
        public static string GetHtml()
        {
            string link = "<a href=\"{0}\" target=\"_blank\">{1}</a> ";
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, OAuth2Base> ob in OAuth2Factory.ServerList)
            {
                if (!string.IsNullOrEmpty(ob.Value.AppKey))
                {
                    sb.AppendFormat(link, string.Format(ob.Value.OAuthUrl, ob.Value.AppKey, System.Web.HttpUtility.UrlEncode(ob.Value.CallbackUrl), ob.Key), ob.Value.ImgUrl);
                }
            }
            return sb.ToString();
        }

        public bool Authorize(out string account)
        {
            account = string.Empty;
            OAuth2.OAuth2Base ob = OAuth2.OAuth2Factory.Current;//获取当前的授权类型，如果成功，则会缓存到Session中。
            if (ob != null) //说明用户点击了授权，并跳回登陆界面来
            {
                if (ob.Authorize(out account))//检测是否授权成功，并返回绑定的账号（具体是绑定ID还是用户名，你的选择）
                {
                    return true;
                }
            }
            return false;
        }



    }
}
